<?
#     Xepec.ru - социальная сеть
#     Copyright (C) 2008-2009  Fedorchenko Alexander
#
#     This program is free software: you can redistribute it and/or modify
#     it under the terms of the GNU Affero General Public License as
#     published by the Free Software Foundation, either version 3 of the
#     License, or (at your option) any later version.
#
#     This program is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU Affero General Public License for more details.
#
#     You should have received a copy of the GNU Affero General Public License
#     along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#     Autor: Fedorchenko Alexander
#     Mail: sos20@list.ru
#     Date: 6/06/2009
#


function count_fotos($id)
{
	include "../config.php";
	
	$re = mysql_query( "SELECT * FROM picture WHERE autor = '$id'", $dbcnx);
	if (!$re) exit("db error");
	
	$numrows = mysql_num_rows($re);
	if ($numrows > 0)
	{
	
		//mysql_close($dbcnx);
		return $numrows;
	}
	else return FALSE;
}

function add_foto($id, $comment)
{
	include "../config.php";

	$nic_cook = $_COOKIE["xepec_nic"];
	$pass_cook = $_COOKIE["xepec_pass"];
	
	$re = mysql_query( "SELECT id FROM reg WHERE name = '$nic_cook' and md5 = '$pass_cook'", $dbcnx);
	if (!$re) exit("db error");
	
	$numrows = mysql_num_rows($re);
	if ($numrows == 1)
	{
	
		$uname_auth = mysql_result($re,0,'id');
		if(count_fotos($uname_auth) == FALSE)
		{
			$count_fotos_new = 1;
		}
		else
		{
			$count_fotos_new = count_fotos($uname_auth) + 1;
		}
		#FIXME: Здесь ошибка запроса не добовляется запись
		#работает если закоментить 35 строчку (mysql_close($dbcnx); в функции count_fotos($id) )
		$re = mysql_query("INSERT INTO picture VALUES (0, $uname_auth, $count_fotos_new, '$comment')", $dbcnx);
		if (!$re) exit("db error");
		
		mysql_close($dbcnx);
		return TRUE;
		
	}
	mysql_close($dbcnx);
	return FALSE;
}

function add_foto_comment($id, $str)
{
	include "../config.php";

	$nic_cook = $_COOKIE["xepec_nic"];
	$pass_cook = $_COOKIE["xepec_pass"];
	
	$re = mysql_query( "SELECT id FROM reg WHERE name = '$nic_cook' and md5 = '$pass_cook'", $dbcnx);
	if (!$re) exit("db error");
	
	$numrows = mysql_num_rows($re);
	if ($numrows == 1)
	{
	
		$uname_auth = mysql_result($re,0,'id');
		
		
		$re = mysql_query( "UPDATE picture SET comment='$str' WHERE autor='$uname_auth' and id_foto='$id'", $dbcnx);
		if ($re)
		{
			mysql_close($dbcnx);
			return TRUE;
		}
	}
	mysql_close($dbcnx);
	return FALSE;
}
?>